package com.penghaisoft.microwms.product.web.controller.insidewarehouse;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.base.model.BaseDictionary;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.util.ExcelExpUtil;
import com.penghaisoft.microwms.common.wms.model.ProContractChange;
import com.penghaisoft.microwms.product.service.IBaseDictionaryService;
import com.penghaisoft.microwms.product.service.IProContractChangeService;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(value = "/proContractChange")
@Slf4j
public class ProContractChangeController extends BaseController {

	@Autowired
	private IProContractChangeService proContractChangeService;
	
	@Autowired
	private IBaseDictionaryService baseDictionaryService;

	/**
	 * 新增记录
	 * 
	 * @param proContractChange
	 * @return
	 */
	@PostMapping("c")
	public TransResult create(ProContractChange proContractChange) {
		proContractChange.preInsert();
		Resp resp = proContractChangeService.create(proContractChange);
		return fromResp(resp);
	}

	/**
	 * 删除记录（逻辑删除active_flag=0)
	 * 
	 * @param proContractChange
	 * @return
	 */
	@PostMapping("d")
	public TransResult delete(ProContractChange proContractChange) {
		proContractChange.preUpdate();
		Resp resp = proContractChangeService.delete(proContractChange);
		return fromResp(resp);
	}

	/**
	 * 查询列表
	 * 
	 * @param pager
	 * @return
	 */
	@PostMapping("list")
	public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows, ProContractChange proContractChange) {
		Pager<ProContractChange> result = proContractChangeService.findListByCondition(page, rows, proContractChange);
		return success(result);
	}

	/**
	 * @Title: listExp @Description: 合同变更历史导出 @param @param
	 * rawContractChange @param @param request @param @param response @param @return
	 * 参数说明 @return String 返回类型 @author luoteng @date 2018年10月25日 上午10:45:02 @throws
	 */
	@GetMapping("listExp")
	public String listExp(ProContractChange proContractChange, HttpServletRequest request,
			HttpServletResponse response) {
		List<ProContractChange> dataList = proContractChangeService.findListByConditionList(proContractChange);
		BaseDictionary baseDictionary1 = new BaseDictionary();
		baseDictionary1.setActiveFlag("1");
		baseDictionary1.setDicType("model_type_p");
		List<BaseDictionary> list1 = baseDictionaryService.queryAllByCondition(baseDictionary1);
		if (dataList != null && !dataList.isEmpty()) {
			for (ProContractChange tmpData : dataList) {
				if (list1 != null && !list1.isEmpty()) {
					for (BaseDictionary tmp : list1) {
						// 判断产品类型
						if (tmp.getDicCode().equals(tmpData.getModelType())) {
							tmpData.setModelType(tmp.getDicName());
						}
					}
				}
			}
		}

		List<Map> columnList = new ArrayList<Map>();
		Map<String, String> columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "条码");
		columnMap.put("columnBeanName", "barcode");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "原合同号");
		columnMap.put("columnBeanName", "contractNoOld");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "新合同号");
		columnMap.put("columnBeanName", "contractNoNew");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "型号");
		columnMap.put("columnBeanName", "model");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "型号描述");
		columnMap.put("columnBeanName", "modelDesc");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "型号类型");
		columnMap.put("columnBeanName", "modelType");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "单位");
		columnMap.put("columnBeanName", "unit");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "长度 ");
		columnMap.put("columnBeanName", "amount");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "库位 ");
		columnMap.put("columnBeanName", "wlCode");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "修改人");
		columnMap.put("columnBeanName", "createBy");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "修改时间 ");
		columnMap.put("columnBeanName", "gmtCreate");
		columnList.add(columnMap);

		SXSSFWorkbook sw = ExcelExpUtil.generateWorkbook(dataList, columnList);

		OutputStream out = null;
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String sdfDate = sdf.format(date);
		String fileName = "成品合同变更历史查询" + sdfDate + ".xlsx";// 文件名
		try {
			out = response.getOutputStream();
			response.setContentType("application/force-download");// 设置强制下载不打开
			response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
			sw.write(out);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

//		//记录导出日志
//		BaseUserExportLog exportLog = new BaseUserExportLog();
//		exportLog.setTableName("OdsProKcfp");
//		String expCond = JSON.toJSONString(odsProKcfp);
//		exportLog.setExpcondition(expCond);
//		if (dataList!=null) {
//			exportLog.setExportNum(dataList.size());
//		}else {
//			exportLog.setExportNum(0);
//		}
//		exportLog.setUserDefined1("excel");
//		exportLog.setUserDefined2("库存分配查询");
//		exportLog.setFactoryCode(getCurrentFactoryCode());
//		exportLog.setCreateBy(getLoginName());
//		userService.addExportLog(exportLog);

		return "sucess";
	}

	/**
	 * 查询单条
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("r/{id}")
	public TransResult queryById(@PathVariable String id) {
		ProContractChange proContractChange = proContractChangeService.findById(id);
		return success(proContractChange);
	}

	/**
	 * 修改记录
	 * 
	 * @param proContractChange
	 * @return
	 */
	@PostMapping("u")
	public TransResult update(ProContractChange proContractChange) {
		proContractChange.preUpdate();
		Resp resp = proContractChangeService.update(proContractChange);
		return fromResp(resp);
	}

}
